package com.lzj.server.common.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.IdUtil;
import com.lzj.server.common.service.impl.SmtpMailServiceImpl;
import com.lzj.server.constant.RedisConstant;
import com.lzj.server.utils.RedisUtils;
import com.lzj.server.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Email;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lzj
 * @date 2025/5/22
 * @description 验证码生成模块
 */
@Slf4j
@Component
@Tag(name = "验证码模块")
@RestController
@RequestMapping("vimi/captcha")
public class CaptchaController {
    @Resource
    private SmtpMailServiceImpl smtpMailService;

    /**
     * cn.hutool.captcha.Captcha
     * ├── LineCaptcha    干扰线验证码（默认）
     * ├── CircleCaptcha  圆圈干扰验证码
     * ├── ShearCaptcha   扭曲验证码
     * └── GifCaptcha     动态验证码
     *
     * @return Base64 编码的验证码图片
     */
    @GetMapping("/getCaptchaImage")
    @Operation(
            summary = "获取验证码图片",
            description = "生成并返回一个 Base64 编码的验证码图片，可选指定宽度和高度"
    )
    public Result<?> getCaptchaImage(
            @Parameter(description = "验证码图片宽度，默认 120", example = "120")
            @RequestParam(value = "width", required = false, defaultValue = "120") int width,

            @Parameter(description = "验证码图片高度，默认 40", example = "40")
            @RequestParam(value = "height", required = false, defaultValue = "40") int height
    ) {
        // 1. 生成验证码
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(width, height, 4, 4);

        // 2. 生成唯一 ID
        String captchaId = IdUtil.simpleUUID();

        // 3. 把验证码文本存到 Redis，设置过期时间
        RedisUtils.set(RedisConstant.CAPTCHA_IMAGE_CODE + captchaId,
                captcha.getCode(), RedisConstant.CAPTCHA_IMAGE_CODE_TIME);

        // 4. 转换图片为 Base64
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        captcha.write(outputStream);
        String base64Image = Base64.getEncoder().encodeToString(outputStream.toByteArray());

        // 5. 组装返回对象
        Map<String, Object> data = new HashMap<>();
        data.put("captchaId", captchaId);
        data.put("captcha", "data:image/png;base64," + base64Image);

        // 6. 返回
        return Result.success(data);
    }

    /**
     * 发送邮件验证码
     */
    @GetMapping("/vimi/emailCaptcha")
    @Operation(summary = "发送邮件验证码")
    @Validated
    public Result<?> sendEmailCaptcha(
            @Email(message = "邮箱格式错误")
            @RequestParam("email") String toEmail) {
        smtpMailService.sendEmailCaptcha(toEmail);
        return Result.success();
    }
}
